package com.bupt.base;

import com.bupt.annotation.MParam;
import com.bupt.exception.BaseException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 项目里面的基础支持方法
 *
 * @author breakpoint/赵先生
 * 2020/10/13
 */
public interface BaseDataSupport {

    // 获取当前的日期的字符串
    static String getCurrentDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd");
        return simpleDateFormat.format(new Date());
    }

    // 解析日期的操作
    static Date parseDateStr(@MParam("日期字符串") String source) throws BaseException {
        if (null == source || "".equals(source)) {
            return null;
        }
        String pattern = "";
        if (source.matches("^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$")) {
            pattern = "yyyy-MM-dd";
        } else if (source.matches("^(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$")) {
            pattern = "HH:mm:ss";
        } else if (source.matches("^[1-9]\\d{3}/(0[1-9]|1[0-2])/(0[1-9]|[1-2][0-9]|3[0-1])$")) {
            pattern = "yyyy/MM/dd";
        } else if (source.matches("^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d:[0-5]\\d$")) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        } else {
            throw new BaseException("not match eg:2020-11-11 or 2020-11-11 11:11:11");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        try {
            Date parse = simpleDateFormat.parse(source);
            return parse;
        } catch (ParseException e) {
            //e.printStackTrace();
            throw new BaseException(e.getMessage());
        }
    }

    // 判断是否在数组中
    static boolean valContainsInArr(@MParam("目标的值") String targetVal, @MParam("数组") String[] arr) {
        if (null != arr && arr.length > 0) {
            for (String val : arr) {
                if (null != val && !"".equals(val) && val.equals(targetVal)) {
                    return true;
                }
            }
        }
        return false;
    }

    // 单位的转换操作
    static Double getRateByRateStr(String rateStr) {
        if (null == rateStr || "".equals(rateStr)) return null;
        switch (rateStr) {
            case "155M":
                return 0.125;
            case "622M":
                return 0.5;
            case "GE":
                return 1.0;
            case "2.5G":
                return 2.5;
            case "10G":
                return 10.0;
            case "40G":
                return 40.0;
            case "100G":
                return 100.0;
            default: {
                return 0.0;
            }
        }
    }

    // 获取到子粒度的数目
    static Long getOTNSlotNumByRateStr(String rateStr) {
        if (null == rateStr || "".equals(rateStr)) return null;
        switch (rateStr) {
            case "GE":
                return 1L;
            case "2.5G":
                return 1L;
            case "10G":
                return 4L;
            case "100G":
                return 40L;
            default: {
                return 0L;
            }
        }
    }

    // 获取SDH的数目
    static Long getSdhSlotNumByRateStr(String rateStr) {
        if (null == rateStr || "".equals(rateStr)) return null;
        switch (rateStr) {
            case "155M":
                return 1L;
            case "622M":
                return 4L;
            case "GE":
                return 8L;
            case "2.5G":
                return 16L;
            case "10G":
                return 64L;
            case "100G":
                return 640L;
            default: {
                return 0L;
            }
        }
    }
}
